package ar.com.larctest.forms;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import ar.com.larctest.R;
import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.temp.TMeasurementItem;
import ar.com.larctest.providers.temp.MeasurementTempProvider;
import ar.com.larctest.utils.Constants;

public class PopUpMeasurementActivity extends Activity {
    private static final String TAG = "PopUpMeasurementActivity";

    String actualMeasurement;
    Integer actualHorse;

    EditText etLacticAcid;
    EditText etHeartRate;

    TextView tvRespiratoryRate;
    EditText etRespiratoryRate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_up_measurement);
        Bundle bundle = getIntent().getExtras();

        try
        {
            actualMeasurement =  bundle.getString("actualMeasurement");
            actualHorse =  bundle.getInt("actualHorse");

            //Set Title
            if(actualMeasurement.equals(Constants.STATE_FINISH_COACHING))
                setTitle(getString(R.string.Measurement_Popup_Title) + " " + Constants.STATE_MEASUREMENT_1);
            else if(actualMeasurement.equals(Constants.STATE_MEASUREMENT_1))
                setTitle(getString(R.string.Measurement_Popup_Title) + " " + Constants.STATE_MEASUREMENT_2);
            else if(actualMeasurement.equals(Constants.STATE_MEASUREMENT_2))
                setTitle(getString(R.string.Measurement_Popup_Title) + " " + Constants.STATE_MEASUREMENT_3);

            etLacticAcid = (EditText) findViewById(R.id.lacticAcid);
            etHeartRate  = (EditText) findViewById(R.id.heartRate);

            tvRespiratoryRate = (TextView) findViewById(R.id.tvRespiratoryRate);
            etRespiratoryRate  = (EditText) findViewById(R.id.respiratoryRate);
            if(!actualMeasurement.equals(Constants.STATE_MEASUREMENT_2)) {
                tvRespiratoryRate.setVisibility(View.INVISIBLE);
                etRespiratoryRate.setVisibility(View.INVISIBLE);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    public void onClickSave(View view)
    {
        SQLLARCTESTHelper sqlHelper = SQLLARCTESTHelper.getHelper(this);

        if(etLacticAcid.getText().toString().equals("")) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Measurement_Required_LacticAcid));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else if(etHeartRate.getText().toString().equals("")) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Measurement_Required_HeartRate));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else if(etRespiratoryRate.getVisibility() ==  View.VISIBLE && etRespiratoryRate.getText().toString().equals("")) {
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText(getText(R.string.Measurement_Required_RespiratoryRate));

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
        }
        else
        {
            sqlHelper.dbOpen();

            try
            {
                sqlHelper.beginTransaction();

                MeasurementTempProvider measurementTempProvider = new MeasurementTempProvider(this);
                TMeasurementItem measurementItem = measurementTempProvider.getMeasurementItem(actualHorse);

                if(actualMeasurement.equals(Constants.STATE_FINISH_COACHING)) {
                    measurementItem.setLacticAcid1(Double.parseDouble(etLacticAcid.getText().toString()));
                    measurementItem.setHeartRate1(Double.parseDouble(etHeartRate.getText().toString()));
                    measurementItem.setState(Constants.STATE_MEASUREMENT_1);
                }
                else if(actualMeasurement.equals(Constants.STATE_MEASUREMENT_1)) {
                    measurementItem.setLacticAcid2(Double.parseDouble(etLacticAcid.getText().toString()));
                    measurementItem.setHeartRate2(Double.parseDouble(etHeartRate.getText().toString()));
                    measurementItem.setState(Constants.STATE_MEASUREMENT_2);
                }
                else if(actualMeasurement.equals(Constants.STATE_MEASUREMENT_2)) {
                    measurementItem.setLacticAcid3(Double.parseDouble(etLacticAcid.getText().toString()));
                    measurementItem.setHeartRate3(Double.parseDouble(etHeartRate.getText().toString()));
                    measurementItem.setRespiratoryRate3(Double.parseDouble(etRespiratoryRate.getText().toString()));
                    measurementItem.setState(Constants.STATE_MEASUREMENT_3);
                }

                measurementTempProvider.updateMeasurementItem(measurementItem);

                sqlHelper.commitTransaction();

                setResult(RESULT_OK);
                this.finish();
            }
            catch (Exception e) {
                sqlHelper.rollbackTransaction();
                Log.e(TAG, e.toString());
            }
            finally {
                sqlHelper.dbClose();
            }
        }
    }

    public void onClickExit(View view)
    {
        setResult(RESULT_CANCELED);
        this.finish();
    }
}
